package so.sao.shop.supplier.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import so.sao.shop.supplier.config.Constant;
import so.sao.shop.supplier.domain.User;
import so.sao.shop.supplier.domain.account.AfterSetting;
import so.sao.shop.supplier.pojo.Result;
import so.sao.shop.supplier.pojo.input.DeliverGoodsInput;
import so.sao.shop.supplier.pojo.input.ExchangePurchaseItemInput;
import so.sao.shop.supplier.pojo.input.ReturnPurchaseSelectInput;
import so.sao.shop.supplier.service.ReturnAndExchangeService;
import so.sao.shop.supplier.service.account.AfterSettingService;
import so.sao.shop.supplier.util.JwtTokenUtil;
import so.sao.shop.supplier.util.Ognl;
import java.util.Map;

/**
 * <p>Version: New shop-1.2.0 V1.1.0 </p>
 * <p>Title: ReturnAndExchangePurchaseItemController</p>
 * <p>Description: 订单退换货controller</p>
 *
 * @author: zhaoyan
 * @Date: Created in 2017/11/29 9:47
 */
@RestController
@RequestMapping(value = "/returnAndExchange")
public class
ReturnAndExchangeController {

    @Autowired
    private ReturnAndExchangeService returnAndExchangeService;

    @Autowired
    private AfterSettingService afterSettingService;

    /**
     * 查询换货列表--供应商与管理员通用
     * @param exchangePurchaseItemInput 换货列表入参对象
     * @return result
     * @throws Exception
     */
    @GetMapping(value = "/exchangeSearch")
    public Result search(ExchangePurchaseItemInput exchangePurchaseItemInput) throws Exception {
        //获取当前登录user
        User user = JwtTokenUtil.getCurrentUser();
        //判断是否登陆
        if (Ognl.isNull(user)) {
            return Result.fail(Constant.MessageConfig.MSG_USER_NOT_LOGIN);
        }
        //非管理员登录设置该账户的storeId
        if (!Constant.ADMIN_STATUS.equals(user.getIsAdmin())) {
            exchangePurchaseItemInput.setStoreId(user.getAccountId());
        }
        return returnAndExchangeService.searchExchange(exchangePurchaseItemInput);
    }

    /**
     * 退货列表
     * @param returnPurchaseSelectInput 退货列表入参对象
     * @return Result
     */
    @GetMapping(value = "/searchReturnItem")
    public Result search(ReturnPurchaseSelectInput returnPurchaseSelectInput){
        //获取当前登陆账户
        User user = JwtTokenUtil.getCurrentUser();
        //判断是否登陆
        if (Ognl.isEmpty( user)) {
            return Result.fail(Constant.MessageConfig.MSG_USER_NOT_LOGIN);
        }
        //非管理员登录设置该账户的storeId
        if (!Constant.ADMIN_STATUS.equals(user.getIsAdmin())) {
            returnPurchaseSelectInput.setStoreId(user.getAccountId());
        }
        return returnAndExchangeService.searchOrders(returnPurchaseSelectInput);
    }

    /**
     * 查询退换货详情信息
     * @param detailsId 详情编号ID
     * @return result
     * @throws Exception
     */
    @GetMapping(value = "/details/{detailsId}")
    public Result getDetail(@PathVariable Long detailsId) throws Exception {
        //获取当前登录user
        User user = JwtTokenUtil.getCurrentUser();
        //判断是否登陆
        if (Ognl.isNull(user)) {
            return Result.fail(Constant.MessageConfig.MSG_USER_NOT_LOGIN);
        }
        return returnAndExchangeService.getReturnAndExchangeDetail(detailsId);
    }

    /**
     * 子订单退换货审核
     * @param map 参数
     * @return result
     * @throws Exception
     */
    @PutMapping(value = "/audit")
    @Transactional(rollbackFor = Exception.class)
    public Result auditPurchaseItem(@RequestBody Map map) throws Exception {
        //获取当前登录user
        User user = JwtTokenUtil.getCurrentUser();
        //判断是否登陆
        if (Ognl.isNull(user)) {
            return Result.fail(Constant.MessageConfig.MSG_USER_NOT_LOGIN);
        }
        if (Ognl.isEmpty(map) || Ognl.isEmpty(map.get("detailsId")) || Ognl.isEmpty(map.get("status")) || Ognl.isEmpty(map.get("storeId"))) {
            return Result.fail(Constant.MessageConfig.PARAMETER_ABNORMITY);
        }
        int status = (int)map.get("status");
        if (status != Constant.OrderDetailsStatusConfig.PASSED && status != Constant.OrderDetailsStatusConfig.REJECT) {
            return Result.fail("审核结果只能为审核通过或审核拒绝！");
        }
        if (status == Constant.OrderDetailsStatusConfig.REJECT && Ognl.isEmpty(map.get("refuseReason"))) {
            return Result.fail("审核拒绝时拒绝原因需必填！");
        }
        if (status == Constant.OrderDetailsStatusConfig.REJECT && ((String)map.get("refuseReason")).length() > Constant.ReturnAndExchangeAudit.REFUSE_REASON_MAX_LENGTH) {
            return Result.fail("审核拒绝原因不能超过" + Constant.ReturnAndExchangeAudit.REFUSE_REASON_MAX_LENGTH + "字符！");
        }
        //审核通过是&&校验StoreId对应的退货地址
        AfterSetting address = afterSettingService.getAfterSetting(Long.parseLong(map.get("storeId").toString()));
        if (Ognl.isNull(address) && status == Constant.OrderDetailsStatusConfig.PASSED) {
            return Result.fail(Constant.MessageConfig.NOSERVICEADRESS);
        }
        return returnAndExchangeService.auditPurchaseItem(Long.parseLong(map.get("detailsId").toString()),(Integer) map.get("status"),(String) map.get("refuseReason"));
    }

    /**
     * 添加订单换货寄出货物流信息
     * @param deliverGoodsInput 物流对象入参
     * @return result
     * @throws Exception
     */
    @PostMapping(value = "/deliverGoods")
    @Transactional(rollbackFor = Exception.class)
    public Result deliverGoods(@RequestBody DeliverGoodsInput deliverGoodsInput) throws Exception {
        //获取当前登录user
        User user = JwtTokenUtil.getCurrentUser();
        //判断是否登陆
        if (Ognl.isNull(user)) {
            return Result.fail(Constant.MessageConfig.MSG_USER_NOT_LOGIN);
        }
        return returnAndExchangeService.deliverGoods(deliverGoodsInput);
    }
}
